;
var helper = require("../../javascript/helper");
var request = require("request");
var db = require("../../javascript/mysql");
var mysql = new db.mysqlClient("accident");
var spider = function () {
    var that = this;
    this.name = "轻松保（境内旅行）"
    // 生成产品信息
    this.genProductInfo = function () {
        let days = [];
        let projects = [];
        for (i = 1; i < 31; i++) {
            days.push(i.toString() + "d")
        }
        plan = {
            planA: {
                "year": ["0-80"],
                "days": days,
                "people_count": ["1"],
                "accident_death": ["100000"],
                "acute_death": ["30000"],
                "travel_medical": ["20000"],
                "travel_hospital": [""],
                "accident_ambulance": [""],
                "airplane_death": ["300000"],
                "train_death": ["300000"],
                "bus_death": ["50000"]
            },
            planB: {
                "year": ["0-80"],
                "days": days,
                "people_count": ["1"],
                "accident_death": ["200000"],
                "acute_death": ["50000"],
                "travel_medical": ["50000"],
                "travel_hospital": ["4500"],
                "accident_ambulance": ["300"],
                "airplane_death": ["500000"],
                "train_death": ["500000"],
                "bus_death": ["100000"]
            },
            planC: {
                "year": ["0-80"],
                "days": days,
                "people_count": ["1"],
                "accident_death": ["300000"],
                "acute_death": ["100000"],
                "travel_medical": ["100000"],
                "travel_hospital": ["9000"],
                "accident_ambulance": ["500"],
                "airplane_death": ["800000"],
                "train_death": ["800000"],
                "bus_death": ["150000"]
            },
            planD: {
                "year": ["0-80"],
                "days": days,
                "people_count": ["1"],
                "accident_death": ["500000"],
                "acute_death": ["150000"],
                "travel_medical": ["200000"],
                "travel_hospital": ["13500"],
                "accident_ambulance": ["1000"],
                "airplane_death": ["1000000"],
                "train_death": ["1000000"],
                "bus_death": ["200000"]
            }

        }
        for (k in plan) {
            data = helper.product([plan[k]["year"], plan[k]["days"], plan[k]["people_count"], plan[k]["accident_death"], plan[k]["acute_death"], plan[k]["travel_medical"], plan[k]["travel_hospital"], plan[k]["accident_ambulance"], plan[k]["airplane_death"], plan[k]["train_death"], plan[k]["bus_death"]])
            for (i = 0; i < data.length; i++) {
                dt = helper.formatProductInfo(data[i], plan[k])
                projects.push(dt)
            }
        }

        return projects;
    }
    //请求数据
    this.httpRequest = function (dt) {

        if (dt.accident_death == "100000") {
            planCd = "B058"
        } else if (dt.accident_death == "200000") {
            planCd = "B061"
        } else if (dt.accident_death == "300000") {
            planCd = "B062"
        } else {
            planCd = "B063"
        }
        params = {
            "pid": "4",
            "skuCode": planCd,
            "device": "pc",
            "age": "80",
            "birthday": "1940-01-01",
            "c_durTime_start": "2020-05-30",
            "durTime": dt.days.replace("d", ""),
        }
        return new Promise(function (resolve, reject) {
            request({
                url: "https://shop.ehuatai.com/huatai/api/product-calc.jsp",
                method: "POST",
                json: false,
                rejectUnauthorized: false,
                headers: {
                    "Accept": "application/json, text/javascript, */*; q=0.01",
                    "Accept-Language": "zh-CN,zh;q=0.9",
                    "Connection": "keep-alive",
                    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
                    "Cookie": "JSESSIONID=B3E7CCA83958D30AF25AF65CEF2E8C7D; _pk_ses.3.3200=1; _pk_id.3.3200=bdfbccf647a93e6d.1583973887.10.1590735162.1590733472.; Hm_lvt_269be4de57d2a3038c3278df47098a88=1590722438,1590728512,1590733471,1590735163; Hm_lpvt_269be4de57d2a3038c3278df47098a88=1590735163; BIGipServershangchengwz-p=3037069578.36895.0000",
                    "Sec-Fetch-Dest": "empty",
                    "Sec-Fetch-Mode": "cors",
                    "Sec-Fetch-Site": "same-origin",
                    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36",
                    "X-Requested-With": "XMLHttpRequest"
                },
                body: helper.urlencode(params)
            }, function (err, res, body) {
                if (!err && res.statusCode == 200) {

                    if (body.indexOf("price") != -1) {
                        price = JSON.parse(body).messages.data.price.toFixed(2).toString();
                        rs = {
                            name: that.name,
                            company: "华泰保险",
                            price: price
                        }
                        resolve(rs);
                    }
                }

            })
        })

    }
    //执行程序
    this.main = async function () {
        let filter = [];
        data = that.genProductInfo();
        for (i = 0; i < data.length; i++) {
            dt = data[i]
            let rs = await that.httpRequest(dt)
            price = rs.price;
            if (!filter.includes(price)) {
                filter.push(price)
                result = Object.assign( dt,rs )
                mysql.insertData( result );
            }

        }
        mysql.close();
    }
}
